package ywpt.service.config;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.LinkmanType;
import ywpt.web.common.PublicFunction;

public class LinkmanTypeServiceImpl extends HibernateEntityDao<LinkmanType> implements LinkmanTypeService{
	public LinkmanTypeServiceImpl(){
		
	}

	
	@Override
	public String CreateTypeCode() {
		DetachedCriteria query = DetachedCriteria.forClass(LinkmanType.class, "linkmanType");
		String typeCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<LinkmanType> linkmanTypes = this.getLinkmanTypes(query);
		if ( linkmanTypes.iterator().hasNext() )	
		{
			code = linkmanTypes.iterator().next().getCode();
			maxId = new Integer(code);
		}
		typeCode=PublicFunction.getNextCode("", maxId, false,3); 
		query.setProjection(null);
		return typeCode;
	}

	@Override
	public LinkmanType getLinkmanTypeById(Long id) {
		return this.get(id);
	}

	@Override
	public int getLinkmanTypeCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LinkmanType> getLinkmanTypes(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public List<LinkmanType> getLinkmanTypes() {
		return this.getAll();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LinkmanType> getLinkmanTypesByTypeName(String typeName, int start,
			int size) {
		DetachedCriteria query = DetachedCriteria.forClass(LinkmanType.class, "linkmanType");
		if ( typeName != null && !typeName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("typeName", "%" + typeName + "%"),  
							          Restrictions.like("code", "%" + typeName + "%")));
		}
		query.addOrder(Order.asc("code"));
		List<LinkmanType> linkmanTypeList = new ArrayList<LinkmanType>();
		List<LinkmanType> linkmanTypeLists = new ArrayList<LinkmanType>();
		LinkmanType linkmanType =new LinkmanType();
		
		linkmanTypeList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<LinkmanType> itr = linkmanTypeList.iterator(); itr.hasNext(); i++ ) 
		{
			linkmanType = itr.next();
			String ss = PublicFunction.format(linkmanType.getCode() + "", 6, "  ", 1)
			          + linkmanType.getTypeName(); 
			linkmanType.setTypeName(ss);
			linkmanTypeLists.add(linkmanType);
			
		}
		return linkmanTypeLists;
	}

	@Override
	public int getLinkmanTypesCountByName(String typeName) {
		DetachedCriteria query = DetachedCriteria.forClass(LinkmanType.class, "linkmanType");
		if ( typeName != null && !typeName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("typeName", "%" + typeName + "%" ),  
							          Restrictions.like("code", "%" + typeName + "%" )));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}

	@Override
	public void removeLinkmanType(Long id) {
		super.removeById(id);
	}

	@Override
	public void saveLinkmanType(LinkmanType linkmanType) {
		super.save(linkmanType);
	}

	@Override
	public void updateLinkmanType(LinkmanType linkmanType) {
		super.update(linkmanType);
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<LinkmanType> getLinkmanTypes(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}
}
